package com.evolve.autotest.cli;

import org.apache.log4j.Appender;
import org.apache.log4j.Layout;
import org.apache.log4j.Level;
import org.apache.log4j.spi.ErrorHandler;
import org.apache.log4j.spi.Filter;
import org.apache.log4j.spi.LoggingEvent;

public class ConsolAppender implements Appender {

	Object lock = new Object();
	
	public void addFilter(Filter arg0) {
	}

	public void clearFilters() {
	}

	public void close() {
	}

	public void write(String message) {
		System.out.print(message);
	}
	
	private void writeln(String message, boolean error) {
		
			if (error) {
				// putting "err" makes output unsynched.
				System.out.println(message);
			} else {
				System.out.println(message);
			}
	}
	
	public void writeln(String message) {
		writeln(message, false);
	}
	
	public void doAppend(LoggingEvent event) {
		if (event.getLevel().isGreaterOrEqual(Level.DEBUG)) {
			
			if (event.getLevel() == Level.ERROR) {
				writeln(event.getMessage().toString(), true);
				
				String message = "";
				
				for (String line : event.getThrowableStrRep()) {
					message += line + "\n";
				}
			} else {
				writeln(event.getMessage().toString(), false);
			}
		}
	}

	public ErrorHandler getErrorHandler() {
		return null;
	}

	public Filter getFilter() {
		return null;
	}

	public Layout getLayout() {
		return null;
	}

	public String getName() {
		return null;
	}

	public boolean requiresLayout() {
		return false;
	}

	public void setErrorHandler(ErrorHandler arg0) {
	}

	public void setLayout(Layout arg0) {
	}

	public void setName(String arg0) {
	}
}
